package com.scexample.common;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.scexample.exception.BusinessException;

import java.util.Collection;
import java.util.Objects;

/**
 * @program: merchantbuilddesign
 * @description: 断言校验
 * @author: 少男
 * @create: 2021-02-26 15:04
 **/
public class BizAssert {
    public static void collectionNotEmpty(Collection<?> collection) {
        if(CollectionUtils.isEmpty(collection)) {
            throw new BusinessException(ErrorCode.S400.statusCode(),ErrorCode.S400.message());
        }
    }

    public static void collectionNotEmpty(Collection<?> collection, String errorCode) {
        if(CollectionUtils.isEmpty(collection)) {
            throw new BusinessException(errorCode, ErrorCode.S400.message());
        }
    }

    public static void collectionNotEmpty(Collection<?> collection, String errorCode, String errorMessage) {
        if(CollectionUtils.isEmpty(collection)) {
            throw new BusinessException(errorCode, errorMessage);
        }
    }

    public static void notNull(Object obj)  {
        if (null== obj) {
            throw new BusinessException(ErrorCode.S400.statusCode(),ErrorCode.S400.message());
        }
    }

    public static void notEmpty(String str) {
        if(StrUtil.isEmpty(str)){
            throw new BusinessException(ErrorCode.S400.statusCode(),ErrorCode.S400.message());
        }

    }

    public static void isBlank(String str) {
        if(StrUtil.isBlank(str)){
            throw new BusinessException(ErrorCode.S400.statusCode(),ErrorCode.S400.message());
        }
    }

    public static void isBlank(String str, String errorMessage) {
        if(StrUtil.isBlank(str)){
            throw new BusinessException(ErrorCode.S400.statusCode(), errorMessage);
        }
    }

    public static void isNullOr(Object... objs) {
        if(objs.length == 1) {
            if(objs[0] instanceof String) {
                isBlank((String) objs[0]);
            } else {
                notNull(objs[0]);
            }
        } else if(objs.length > 1) {
            for (Object obj : objs) {
                if(obj instanceof String) {
                    isBlank((String) obj);
                } else {
                    notNull(obj);
                }
            }
        }
    }

    public static void isNullOr(String errorMessage, Object... objs) {
        if(objs.length == 1) {
            if(objs[0] instanceof String) {
                isBlank((String) objs[0], errorMessage);
            } else {
                notNull(objs[0], errorMessage);
            }
        } else if(objs.length > 1) {
            for (Object obj : objs) {
                if(obj instanceof String) {
                    isBlank((String) obj, errorMessage);
                } else {
                    notNull(obj, errorMessage);
                }
            }
        }
    }

    public static void notEmpty(Collection collection) {
        if (collection == null || collection.isEmpty()) {
            throw new BusinessException(ErrorCode.S400.statusCode(),ErrorCode.S400.message());
        }
    }

    /**
     * 条件不成立为false时，抛出指定message的业务异常
     * @param condition 条件
     * @param errorMessage 错误消息
     */
    public static void isTrue(boolean condition,String errorMessage)  {
        if (!condition) {
            throw new BusinessException(ErrorCode.S501.statusCode(),errorMessage);
        }
    }

    /**
     * 条件不成立为false时，抛出指定message的业务异常
     * @param condition 条件
     * @param errorCode 错误消息
     */
    public static void isTrue(boolean condition,ErrorCode errorCode)  {
        if (!condition) {
            throw new BusinessException(errorCode.statusCode(),errorCode.message());
        }
    }

    /**
     * 条件不成立为false时，抛出指定message的业务异常
     *
     * @param condition 条件
     * @param errorMessage 错误消息
     * @param statusCode 错误编码
     */
    public static void isTrue(boolean condition, String errorMessage, String statusCode) {
        if (!condition) {
            throw new BusinessException(statusCode, errorMessage);
        }
    }

    public static void isFalse(boolean condition,String errorMessage)  {
        if (condition) {
            throw new BusinessException(ErrorCode.S501.statusCode(),errorMessage);
        }
    }

    public static void notNull(Object obj,String errorMessage)  {
        isTrue(obj!=null,errorMessage);
    }

    public static void notNull(Object obj, String errorCode, String errorMessage)  {
        if(Objects.isNull(obj)) {
            throw new BusinessException(errorCode, errorMessage);
        }
    }

    public static void notEmpty(String str,String errorMessage) {
        isTrue(StrUtil.isNotEmpty(str),errorMessage);
    }

    public static void notEmpty(Collection collection,String errorMessage) {
        isTrue(CollectionUtils.isNotEmpty(collection),errorMessage);
    }

    public static void isEmpty(Collection collection) {
        if (!CollectionUtils.isEmpty(collection)) {
            throw new BusinessException(ErrorCode.S418.statusCode(),ErrorCode.S418.message());
        }

    }

    /**
     * throw异常
     */
    public static void throwBusinessException(ErrorCode errorCode) {
        throw new BusinessException(errorCode.statusCode(),errorCode.message());
    }

    /**
     * throw异常
     */
    public static void throwBusinessException(String code, String message) {
        throw new BusinessException(code, message);
    }
}
